﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 菱声音响文字转语音
    /// </summary>
    //public class LS20BLL
    //{
    //    string type = "req";
    //    string app = "ls20";
    //    string uid = "";

    //    public LS20BLL()
    //    {
    //        Login();
    //    }
    //    public Models.ReceiveDataBase<object> Login(string account="admin",string passWord="admin")
    //    {

    //        Models.LSwzzyybfM.loginReq loginReq = new Models.LSwzzyybfM.loginReq();
    //        loginReq.type = type;
    //        loginReq.app = app;
    //        loginReq.op = "login";
    //        loginReq.params1.name = account;
    //        loginReq.params1.password = passWord;
    //        loginReq.params1.vender = app;
    //        Models.LSwzzyybfM.loginRes loginRes = ZK.InterfaceServer.LSwzzyybfServer.login(loginReq);
    //        if (loginRes.params1.result!= "0")
    //        {
    //            return Models.ReceiveDataBase<object>.SetFailRes("登录失败");
    //        }
    //        uid =  loginRes.params1.uid;
    //        return Models.ReceiveDataBase<object>.SetSuccessRes("登录成功");
    //    }
    //    /// <summary>
    //    /// 发送信息给音响
    //    /// </summary>
    //    /// <param name="msg"></param>
    //    /// <param name="sn">需要提前知道添加的设备后的设备名称</param>
    //    /// <param name="origin"></param>
    //    /// <param name="voice_name"></param>
    //    /// <param name="speed"></param>
    //    /// <param name="volume"></param>
    //    /// <param name="count"></param>
    //    /// <param name="vol"></param>
    //    /// <returns></returns>
    //    public Models.ReceiveDataBase<object> SendMessage(
    //        string msg,
    //        string[] sn,
    //        string origin = "http://192.168.1.188:10008",
    //        string voice_name = "xiaoyan",
    //        string speed = "30",
    //        string volume = "100",
    //        int count = 1,
    //        int vol = 25
    //        )
    //    {
    //        Models.LSwzzyybfM.tts_xf mm = new Models.LSwzzyybfM.tts_xf();
    //        mm.text = msg;
    //        mm.origin = origin;
    //        mm.speed = speed;
    //        mm.voice_name = voice_name;
    //        mm.volume = volume;
    //        Models.LSwzzyybfM.tts_xfRes res = ZK.InterfaceServer.LSwzzyybfServer.tts_xf(mm);
    //        if (res.result!= "0")
    //        {
    //            return Models.ReceiveDataBase<object>.SetFailRes("语音转化失败");
    //        }
    //        //播放
    //        Models.LSwzzyybfM.fileUrl fileUrl1 = new Models.LSwzzyybfM.fileUrl();
    //        fileUrl1.name = "name1";
    //        fileUrl1.uri = res.url;
    //        List<Models.LSwzzyybfM.fileUrl> fileUrls = new List<Models.LSwzzyybfM.fileUrl>();
    //        fileUrls.Add(fileUrl1);
    //        Models.LSwzzyybfM.playMusicReq playMusic = new Models.LSwzzyybfM.playMusicReq();
    //        playMusic.type = type;
    //        playMusic.app = app;
    //        playMusic.op = "playMusic";
    //        playMusic.params1.uid =uid;
    //        playMusic.params1.name = "task1";
    //        playMusic.params1.type = 5;
    //        playMusic.params1.devices = sn;
    //        playMusic.params1.level = 1000;
    //        playMusic.params1.leng = "0";
    //        playMusic.params1.count = count;
    //        playMusic.params1.vol = vol;
    //        playMusic.params1.urls = fileUrls;
    //        Models.LSwzzyybfM.playMusicRes  playMusic1 = ZK.InterfaceServer.LSwzzyybfServer.playMusic(playMusic);
    //        if (playMusic1.params1.result != "0")
    //        {
    //            return Models.ReceiveDataBase<object>.SetFailRes("播放失败");
    //        }
    //        return Models.ReceiveDataBase<object>.SetSuccessRes("语音发送并播放成功");
    //    }
    //}

    public class LS20BLL
    {
        string type = "req";
        string app = "ls20";
        string uid = "";
        string wjyyPort = "10008";
        Models.TerminalM yx;
        Models.TerminalM yxfw;
        string[] sns;

        public LS20BLL(Models.TerminalM yxfw, Models.TerminalM yx)
        {
            this.yx = yx;
            this.yxfw = yxfw;
            InterfaceServer.LSwzzyybfServer.yxfwIp = yxfw.ip;
            Common.ShowBlackBox.WriteLine("1 服务器ip：" + InterfaceServer.LSwzzyybfServer.yxfwIp);
            Login();
            getAllDevices();
        }
        public Models.ReceiveDataBase<object> Login(string account = "admin", string passWord = "admin")
        {

            Models.LSwzzyybfM.loginReq loginReq = new Models.LSwzzyybfM.loginReq();
            loginReq.type = type;
            loginReq.app = app;
            loginReq.op = "login";
            loginReq.params1.name = account;
            loginReq.params1.password = passWord;
            loginReq.params1.vender = app;
            Common.ShowBlackBox.WriteLine(" 1 账号：" + Newtonsoft.Json.JsonConvert.SerializeObject(loginReq));
            Common.ShowBlackBox.WriteLine("1 登录入参" + Newtonsoft.Json.JsonConvert.SerializeObject(loginReq));
            Models.LSwzzyybfM.loginRes loginRes = ZK.InterfaceServer.LSwzzyybfServer.login(loginReq);
            if (loginRes.params1.result != "0")
            {
                return Models.ReceiveDataBase<object>.SetFailRes("登录失败");
            }
            uid = loginRes.params1.uid;
            Common.ShowBlackBox.WriteLine("1 登录出参" + Newtonsoft.Json.JsonConvert.SerializeObject(loginRes));
            return Models.ReceiveDataBase<object>.SetSuccessRes("登录成功");
        }

        public Models.ReceiveDataBase<object> getAllDevices()
        {
            Models.LSwzzyybfM.getAllDevicesReq mm = new Models.LSwzzyybfM.getAllDevicesReq();
            mm.type = type;
            mm.app = app;
            mm.op = "getAllDevices";
            mm.params1.uid = uid;
            Models.LSwzzyybfM.getAllDevicesRes loginRes = ZK.InterfaceServer.LSwzzyybfServer.getAllDevices(mm);
            Common.ShowBlackBox.WriteLine("1 设备列表入参" + Newtonsoft.Json.JsonConvert.SerializeObject(mm));
            if (loginRes.params1.result != "0")
            {
                return Models.ReceiveDataBase<object>.SetFailRes("获取设备列表失败");
            }
            sns = loginRes.params1.devices.Where(p => p.ip == this.yx.ip).Select(p => p.sn).ToArray();
            Common.ShowBlackBox.WriteLine("1 设备列表出参" + Newtonsoft.Json.JsonConvert.SerializeObject(loginRes));
            return Models.ReceiveDataBase<object>.SetSuccessRes();
        }


        public void DevPlayStop()
        {
            Models.LSwzzyybfM.devPlayStopReq devPlayStopReq = new Models.LSwzzyybfM.devPlayStopReq();
            devPlayStopReq.type = "req";
            devPlayStopReq.app = "ls20";
            devPlayStopReq.op = "devPlayStop";
            devPlayStopReq.params1.devices = sns;
            devPlayStopReq.params1.uid = uid;
            Common.ShowBlackBox.WriteLine("1 停止入参" + Newtonsoft.Json.JsonConvert.SerializeObject(devPlayStopReq));
            var aa =  InterfaceServer.LSwzzyybfServer.devPlayStop(devPlayStopReq);
            Common.ShowBlackBox.WriteLine("1 停止出参" + Newtonsoft.Json.JsonConvert.SerializeObject(aa));
        }

        /// <summary>
        /// 发送信息给音响
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="origin"></param>
        /// <param name="voice_name"></param>
        /// <param name="speed"></param>
        /// <param name="volume"></param>
        /// <param name="count"></param>
        /// <param name="vol"></param>
        /// <returns></returns>
        public Models.ReceiveDataBase<object> SendMessage(
            string msg,
            string voice_name = "xiaoyan",
            string speed = "35",
            string volume = "100",
            int count = 2,
            int vol = 25
            )
        {
            try
            {
                if (sns != null && sns.Count() < 1)
                {
                    Common.ShowBlackBox.WriteLine("3 设备不能为空");
                    return Models.ReceiveDataBase<object>.SetFailRes("设备不能为空");
                }
                Models.LSwzzyybfM.tts_xf mm = new Models.LSwzzyybfM.tts_xf();
                mm.text = msg;
                mm.origin = "http://" + this.yxfw.ip + ":" + wjyyPort;
                mm.speed = speed;
                mm.voice_name = voice_name;
                mm.volume = volume;
                Common.ShowBlackBox.WriteLine("1 转语音入参" + Newtonsoft.Json.JsonConvert.SerializeObject(mm));
                Models.LSwzzyybfM.tts_xfRes res = ZK.InterfaceServer.LSwzzyybfServer.tts_xf(mm);
                Common.ShowBlackBox.WriteLine("1 转语音出参" + Newtonsoft.Json.JsonConvert.SerializeObject(res));
                if (res.result != "0")
                {
                    Common.ShowBlackBox.WriteLine("3 语音转化失败");
                    return Models.ReceiveDataBase<object>.SetFailRes("语音转化失败");
                }
                Common.ShowBlackBox.WriteLine("1 播放ing");
                //先暂停
                DevPlayStop();
                //播放
                Models.LSwzzyybfM.fileUrl fileUrl1 = new Models.LSwzzyybfM.fileUrl();
                fileUrl1.name = "name1";
                fileUrl1.uri = res.url;
                List<Models.LSwzzyybfM.fileUrl> fileUrls = new List<Models.LSwzzyybfM.fileUrl>();
                fileUrls.Add(fileUrl1);
                Models.LSwzzyybfM.playMusicReq playMusic = new Models.LSwzzyybfM.playMusicReq();
                playMusic.type = type;
                playMusic.app = app;
                playMusic.op = "playMusic";
                playMusic.params1.uid = uid;
                playMusic.params1.name = "task1";
                playMusic.params1.type = 5;
                playMusic.params1.devices = sns;
                playMusic.params1.level = 1000;
                playMusic.params1.leng = "";
                playMusic.params1.count = count;
                playMusic.params1.vol = vol;
                playMusic.params1.urls = fileUrls;
                Common.ShowBlackBox.WriteLine("1 播放入参" + Newtonsoft.Json.JsonConvert.SerializeObject(playMusic));
                Models.LSwzzyybfM.playMusicRes playMusic1 = ZK.InterfaceServer.LSwzzyybfServer.playMusic(playMusic);
                Common.ShowBlackBox.WriteLine("1 播放出参" + Newtonsoft.Json.JsonConvert.SerializeObject(playMusic1));
                if (playMusic1.params1.result != "0")
                {
                    Common.ShowBlackBox.WriteLine("3 播放失败");
                    return Models.ReceiveDataBase<object>.SetFailRes("播放失败");
                }
                Common.ShowBlackBox.WriteLine("1 语音发送并播放成功");
                return Models.ReceiveDataBase<object>.SetSuccessRes("语音发送并播放成功");
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("播放音响文件时发生了异常:" + ex);
                Common.ShowBlackBox.WriteLine("3 播放音响文件时发生了异常");
                return Models.ReceiveDataBase<object>.SetFailRes("播放音响文件时发生了异常");
            }
        }
    }
}
